package com.wangxiang.shop.product.controller;

import java.util.*;


import com.wangxiang.common.utils.PageEntity;
import com.wangxiang.shop.product.entity.*;
import com.wangxiang.shop.product.feign.SysFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wangxiang.shop.product.service.SpuService;
import com.wangxiang.common.utils.PageUtils;
import com.wangxiang.common.utils.R;



/**
 * 
 *
 * @author wangxiang
 * @email 2293672863@qq.com
 * @date 2022-04-02 11:30:18
 */
@RestController
@RequestMapping("product/spu")
public class SpuController {
    @Autowired
    private SpuService spuService;
    @Autowired
    SysFeign sysFeign;

    /**
     * 会员服务调用
     * @param shopCart
     * @return
     */
    @RequestMapping("member/cart")
    public List<MemberCartEntity> MemberCart(@RequestBody List<ShopCartEntity> shopCart){

        List<MemberCartEntity> cart = spuService.getCart(shopCart);
//        System.out.println("会员服务调用"+cart);
        return cart;
    }
    @RequestMapping("page")
    public R page(@RequestBody PageEntity pageEntity){
        PageEntity page = spuService.page(pageEntity);
        return R.ok().put("page",page);

    }
    @RequestMapping("bytype")
    public R getSpuByType(@RequestBody HashMap<String , String> map){
        String type = map.get("type");
        List<SpuEntity> spuByType = spuService.getSpuByType(type);
        return R.ok().put("list",spuByType);
    }

    @RequestMapping("searchtitle")
    public R searchtitle(@RequestBody String search){
        if (search==null|search==""){
            return R.error("请输入你要查询的商品");
        }
        List<SpuEntity> search1 = spuService.search(search);
        if (search1.size()==0){
            return R.ok().put("msg","商品尚未收录该商品").put("code",600);
        }
        return R.ok().put("list",search1);
    }
    @RequestMapping("search")
    public R search(@RequestBody HashMap<String,Object> map){
        if (map.get("spuId")==null){
            return R.error("不能空查询，请输入商品id");
        }
        try{
            int spuId = Integer.valueOf(map.get("spuId").toString());
            List<SpuEntity> list = spuService.searchById(spuId);
            if (list!=null){
                return R.ok().put("list",list);
            }else {
                return R.error("商品不存在");
            }

        }catch (NumberFormatException n){
            return R.error("商品id是数值，请输入数值 ");
        }

    }
    //失效商品，spu,sku
    @RequestMapping("invalid")
    public R invalid(@RequestBody Integer id){
        int i = spuService.updateStatus(id, 1);
        if (i>0){
            return R.ok().put("msg","你已失效商品，失效成功").put("code",800);
        }else {
            return R.error("失效错误，系统错误");
        }
    }

    /**
     * 重新上架需要调用系统服务接口
     * @param map
     * @return
     */
    @RequestMapping("recover")
    public R recover(@RequestBody HashMap<String , Integer> map){
        map.put("status",0);
        int i = spuService.updateStatus(map.get("spuId"), 2);
        if (i>0){
            int add = sysFeign.add(map);
            if (add>0){

                return R.ok().put("msg","你已申请商品重新上架，等待超级管理员审核").put("code",800);
            }else {
                return R.error("系统错误");

            }
        }else {
            return R.error("商品申请重新上架失败");
        }


    }
    @RequestMapping("param")
    public R param(@RequestBody Integer id){
        List<HashMap<String, Object>> param = spuService.param(id);
        if (param.size()==0){
            return R.error("系统错误，可能是没有数据");
        }
        return R.ok().put("list",param);
    }
    @RequestMapping("ware")
    public R ware(@RequestBody Integer id){
        List<SkuEntity> wareList = spuService.getWareList(id);
        if (wareList.size()==0) {
            return R.error("该商品库存信息不存在");
        }
        return R.ok().put("list",wareList);
    }

    @RequestMapping("membercart")
    public List<CartEntity> membercart(@RequestBody List<CartEntity> entity){
//        System.out.println(entity);
        List<CartEntity> list = spuService.memberCart(entity);
        return list;
    }

    @RequestMapping("selectAudit")
    public List<HashMap<String , Object>> auditshop(@RequestBody List<HashMap<String , Object>> maps){
        List<HashMap<String, Object>> list = spuService.auditshopService(maps);
        return list;
    }

    @RequestMapping("auditRecover")
    public int auditRecover(@RequestBody HashMap<String , Integer> map){
        int i = spuService.updateStatus(map.get("spuId"), map.get("status"));
        return i;
    }

    @RequestMapping("member/order/list")
    public List<HashMap<String , Object>> memberOrderList(@RequestBody List<HashMap<String , Object>> list){
        List<HashMap<String, Object>> memberOrderList = spuService.memberOrderList(list);
        return memberOrderList;
    }

    /**
     * 商品的添加，本系统前端最为复杂的功能
     * 服务端难道偏高，涉及复杂表单校验，多项表单校验，前端校验复杂所以使用后端校验
     * 沿用上一版本的商品添加形式，一次可添加多库存
     * 复杂格式转换，解析前端数据
     * HashMap->List->LinkedHashMap->HashMap
     * HashMap->double(异常机制表单校验)
     * HashMap->int(异常机制表单校验)
     * 校验项格式，关键参数非空校验
     * 部分使用异常机制拦截错误返回（用于数据格式）
     * 使用http远程调用系统服务
     * 商品服务自定义插入语句，返回自增主键作为sku的spuId字段，
     * spu表自定义
     * sku表自定义
     * @param map
     * @return
     */
    @RequestMapping("add/audit")
    public R addAudit(@RequestBody HashMap<String  , Object> map){
        String nullStr = "";
        //表单校验,商品类型，基础表单
        if (map.get("shopType")==null){
            return R.error("商品类型未选择");
        }
        //商品标题
        if (map.get("shopTitle")==null|nullStr.equals(map.get("shopTitle"))|map.get("shopTitle")==" "){
            return R.error("商品标题为空");
        }
        //品牌
        if (map.get("shopBrand")==null|nullStr.equals(map.get("shopBrand"))|map.get("shopBrand")==" "){
            return R.error("商品品牌为空");
        }
        //店铺
        if (map.get("shopStore")==null|nullStr.equals(map.get("shopStore"))|map.get("shopStore")==" "){
            return R.error("店铺为空");
        }
        //名称
        if (map.get("shopName")==null|nullStr.equals(map.get("shopName"))|map.get("shopName")==" "){
            return R.error("店铺为空");
        }
        //图片
        if (map.get("shopImage")==null|nullStr.equals(map.get("shopImage"))|map.get("shopImage")==" "){
            return R.error("图片错误，可能原因vuex缓存错误");
        }
        //产地
        if (map.get("shopLocation")==null|nullStr.equals(map.get("shopLocation"))|map.get("shopLocation")==" "){
            return R.error("产地为空");
        }

        //参数表单校验
        ArrayList<HashMap<String, Object>> paramList = new ArrayList<>();
        ArrayList<HashMap<Object, Object>> paramMaps = new ArrayList<>();
        if (map.get("paramList")!=null){
            paramList = (ArrayList<HashMap<String, Object>>) map.get("paramList");
            for (int i = 0; i < paramList.size(); i++) {
                if (paramList.get(i).get("key")==null|paramList.get(i).get("value")==null|nullStr.equals(paramList.get(i).get("key"))|nullStr.equals(paramList.get(i).get("value"))){
                    return R.error("非必须表单校验失败,请按顺序填写，key,value不能为空");
                }else {
                    HashMap<Object, Object> paramMap = new HashMap<>();
                    paramMap.put(paramList.get(i).get("key"),paramList.get(i).get("value"));
                    paramMaps.add(paramMap);
                }
            }
        }
//        System.out.println("ddddddddd"+paramMaps);

        //库存
        ArrayList<HashMap<String, Object>> skuList = new ArrayList<>();
        skuList = (ArrayList<HashMap<String, Object>>) map.get("skuList");
        ArrayList<HashMap<String, Object>> skuMaps = new ArrayList<>();
        for (HashMap<String, Object> sku : skuList) {
            try {
                HashMap<String, Object> skuMap = new HashMap<>();
                skuMap.put("style",sku.get("style"));
                skuMap.put("size",sku.get("size"));
                double price = Double.valueOf(sku.get("price").toString());
                int ware = Integer.valueOf(sku.get("ware").toString());
                skuMap.put("price",price);
                skuMap.put("ware",ware);
                skuMaps.add(skuMap);
            }catch (NumberFormatException n){
                return R.error("库存为整数，价格为浮点数，示例库存：18，价格: 9.9");
            }

        }

        int spuId = spuService.addShop(map, paramMaps, skuMaps);
        HashMap<String, Integer> sysMap = new HashMap<>();
        sysMap.put("spuId",spuId);
        sysMap.put("status",1);
        int userId = Integer.valueOf(map.get("userId").toString());
        sysMap.put("userId",userId);
        int add = sysFeign.add(sysMap);
        if (add==0){
            return R.error("http远程调用系统服务错误");
        }

        if (spuId==0|spuId==1|spuId==2|spuId==3){
            return R.error("商品服务流程错误，错误原因可能未spu插入，sku插入，spu更新错误");
        }



        return R.ok().put("msg","成功添加第"+spuId+"件商品").put("code",800);
    }
}
